<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 后台黑名单模型
 * @package app\admin\model
 */
class Blacklist extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_black_list';
    protected $observerClass = 'app\admin\event\DeleteCache';
    //查询黑名单是否存在
    public static function getOne($data)
    {
    	$where = [
    			'cid' => trim($data['cid']),
                'phone' => $data['phone']
    		];
    	return self::get($where);
    }

    //更新
    public static function upd($data,$blackId)
    {
        $data['change_time'] = date('Y-m-d H:i:s',time());
        return self::where(['id'=>$blackId])->update($data);
    }

    public static function addOne($data)
    {
        $data['change_time'] = date('Y-m-d H:i:s', time());
        return self::create($data);
    }

    //黑名单开关
    public static function openOrClose($black_id,$status)
    {
        trace(session('username').'把渠道黑名单id为'.$black_id.'的开启状态改为了'.$status,'info');
        return self::where(['id'=>$black_id])->update(['status' => $status]);
    }

    public static function addAll($data,$arr)
    {
        foreach ($arr as $val) {
            $where = [
                'cid' => $data['cid'],
                'phone' =>$val
            ];
            $row = [
                'cid' => $data['cid'],
                'change_time' => date('Y-m-d H:i:s',time()),
                'phone' =>$val,
                'mark' => $data['mark'],
            ];
            if( !self::where($where)->find() ){
                self::create($row);
            }
        }
        return true;
    }

    /**
     * [getAlls 条件查询全部数据]
     * @author   .guo
     * @DataTime 2019-05-27
     * @param    [type]     $request [条件数组]
     * @return   [type]              [object]
     */
    public static function getAlls($request)
    {
        $where = [];

        if (!empty($request['phone'])) {

            $where['b.phone'] = trim($request['phone']);
        }

        if (!empty($request['cid'])) {

            $where['b.cid'] = trim($request['cid']);
        }

        return self::alias('b')
            ->field('b.id,b.phone,b.change_time,b.mark,b.status,b.cid,bs.company_name')
            ->where($where)
            ->join('bus_channel bs', 'b.cid = bs.channel_id', 'LEFT')
            ->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {

                switch ($item->status) {
                    case '1':
                        $item->status_ = '<span class="layui-badge layui-bg-green status-qy">正常</span>';
                        break;

                    default:
                        $item->status_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }
            });

    }



    public static function getRow($blackId)
    {
        $where = [
                'b.id' => $blackId,
            ];
        return self::alias('b')
            ->field('b.id,b.phone,b.change_time,b.mark,b.status,b.cid,bs.company_name')
            ->where($where)
            ->join('bus_channel bs', 'b.cid = bs.channel_id', 'LEFT')
            ->find();
    }

    //返回用户名
    public function getChannelInfo($data)
    {
        return Db::name('bus_channel')->field('company_name')->where(['channel_id'=> trim($data)])->find();
    }

}
